home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / languages / fpl-v115.lha / FPL / src / reference.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-02  |  7.8 KB  |  241 lines

  1. /******************************************************************************
  2.  *              FREXX PROGRAMMING LANGUAGE                  *
  3.  ******************************************************************************
  4.  
  5.  Reference.c
  6.  
  7.  Routines for interpreting variable references from the interface function.
  8.  
  9.  *****************************************************************************/
  10.  
  11. /************************************************************************
  12.  *                                                                      *
  13.  * fpl.library - A shared library interpreting script langauge.         *
  14.  * Copyright (C) 1992-1994 FrexxWare                                    *
  15.  * Author: Daniel Stenberg                                              *
  16.  *                                                                      *
  17.  * This program is free software; you may redistribute for non          *
  18.  * commercial purposes only. Commercial programs must have a written    *
  19.  * permission from the author to use FPL. FPL is *NOT* public domain!   *
  20.  * Any provided source code is only for reference and for assurance     *
  21.  * that users should be able to compile FPL on any operating system     *
  22.  * he/she wants to use it in!                                           *
  23.  *                                                                      *
  24.  * You may not change, resource, patch files or in any way reverse      *
  25.  * engineer anything in the FPL package.                                *
  26.  *                                                                      *
  27.  * This program is distributed in the hope that it will be useful,      *
  28.  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
  29.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                 *
  30.  *                                                                      *
  31.  * Daniel Stenberg                                                      *
  32.  * Ankdammsgatan 36, 4tr                                                *
  33.  * S-171 43 Solna                                                       *
  34.  * Sweden                                                               *
  35.  *                                                                      *
  36.  * FidoNet 2:201/328    email:dast@sth.frontec.se                       *
  37.  *                                                                      *
  38.  ************************************************************************/
  39.  
  40. #include <stddef.h>
  41. #include "script.h"
  42.  
  43. #if defined(UNIX)
  44. #include <stdio.h>
  45. #endif
  46.  
  47. #include "reference.h"
  48.  
  49. struct fplStr EmptyString={0,0,0};
  50.  
  51. ReturnCode PREFIX
  52. fplReference(AREG(0) struct Data *scr,
  53.              AREG(1) struct Identifier *ident,
  54.              AREG(2) unsigned long *tags)
  55. {
  56.   ReturnCode ret = FPL_OK;
  57.   long item=0;
  58.   long a=0;
  59.   long *dims;
  60.   long my_strlen=-1;
  61.   struct fplRef *ref;
  62.   while(tags && *tags) {
  63.     switch(*tags++) {
  64.     case FPLREF_ARRAY_RESIZE:        /* version 11 magic! */
  65.       if(ident->flags&FPL_VARIABLE && ident->data.variable.num) {
  66.         /*
  67.          * It is a variable and it is at least a one dimensional array!
  68.          */
  69.         ref = (struct fplRef *)(*tags);
  70.         CALL( ArrayResize(scr,
  71.                           ref->Dimensions,
  72.                           ref->ArraySize,
  73.                           ident));
  74.       }
  75.       break;
  76.       
  77.     case FPLREF_ARRAY_ITEM:
  78.       if(ident->flags&FPL_VARIABLE) {
  79.     dims = (long *)(*tags);
  80.     while(dims[a]>0)
  81.       a++;
  82.     item = ArrayNum(a, ident->data.variable.num,
  83.             dims, ident->data.variable.dims);
  84.     if(item<0)
  85.       item=0; /* just ignore stupid values! */
  86.         a=0; /* reset 'a' again */
  87.       }
  88.       break;
  89.     case FPLREF_ARRAY_INFO:
  90.       /*
  91.        * Fill out the structure given to us by the caller!
  92.        */
  93.       if(ident->flags&FPL_VARIABLE && ident->data.variable.num) {
  94.     ref = (struct fplRef *)(*tags);
  95.     ref->Dimensions = ident->data.variable.num;  /* number of dims */
  96.     ref->ArraySize  = ident->data.variable.dims; /* actual dims */
  97.       }
  98.       break;
  99.     case FPLREF_NAME:
  100.       /*
  101.        * Receive name in supplied char pointer!
  102.        */
  103.       *(char **)(*tags) = ident->name;
  104.       break;
  105.     case FPLREF_TYPE:
  106.       /*
  107.        * Receive flags in supplied long!
  108.        */
  109.       *(long *)(*tags) =
  110.         (ident->flags&FPL_STRING_VARIABLE?FPLREF_TYPE_STRING:0)|
  111.       (ident->flags&FPL_INT_VARIABLE?FPLREF_TYPE_INTEGER:0)|
  112.         (ident->flags&FPL_VARIABLE?
  113.          (ident->data.variable.num?FPLREF_TYPE_ARRAY:0):0 )|
  114.            (ident->flags&FPL_FUNCTION?FPLREF_TYPE_FUNCTION:0);
  115.       break;
  116.  
  117.     case FPLREF_GET_STRING:
  118.       if(ident->flags&FPL_STRING_VARIABLE &&
  119.      item<ident->data.variable.size) {
  120.         if(ident->data.variable.var.str[item]) {
  121.           /*
  122.            * This is a string!
  123.            */
  124.           *(char **)(*tags) = ident->data.variable.var.str[item]->string;
  125.         }
  126.         else {
  127.           /*
  128.            * Can't return NULL for no string, return pointer to ""!
  129.            */
  130.           *(char **)(*tags) = EmptyString.string;
  131.         }
  132.       }
  133.       else {
  134.     *(char **)(*tags) = NULL;
  135.       }
  136.       break;
  137.  
  138.     case FPLREF_SET_MY_STRLEN:
  139.       my_strlen=(long)(*tags);
  140.       break;
  141.     case FPLREF_SET_MY_STRING:
  142.       a=1;
  143.     case FPLREF_SET_STRING:
  144.       if(ident->flags&FPL_STRING_VARIABLE &&
  145.      item<ident->data.variable.size &&
  146.          *tags) {
  147.     /*
  148.      * Get the allocation type.
  149.      */
  150.     char type=MALLOC_DYNAMIC; /* default memory state */
  151.     struct fplStr *str;
  152.     if(ident->data.variable.var.str[item]) {
  153.       type = TypeMem(ident->data.variable.var.str[item]);
  154.  
  155.       if(MALLOC_STATIC == type ) {
  156.         /*
  157.          * The previous allocation was static!
  158.          */
  159.         FREEA( ident->data.variable.var.str[item] );
  160.       }
  161.           else {
  162.             FREE( ident->data.variable.var.str[item] );
  163.           }
  164.     }
  165.     if(1 == a) {
  166.       /*
  167.        * This is a FPLREF_SET_MY_STRING call. That means we got a regular
  168.        * C style char pointer to a zero terminated string here, allocate
  169.        * a regular FPL string and copy the string into that one
  170.        */
  171.       register long len = 0>my_strlen?strlen((char *)*tags):my_strlen;
  172.           GETMEM(str, len + sizeof(struct fplStr));
  173.           str->alloc= len;
  174.           str->len= len; /* set default to entire string */
  175.  
  176.           /* copy the data into the string */
  177.           memcpy(str->string, (char *)(*tags), len);
  178.     }
  179.     else {
  180.           /*
  181.            * We received a pointer to the ->string member of a fplStr struct.
  182.            * Make 'str' point to the struct itself!
  183.            */
  184.       str = (struct fplStr *)
  185.         (((char *)(*tags)) - offsetof(struct fplStr, string));
  186.         }
  187.  
  188.     ident->data.variable.var.str[item] = str;
  189.  
  190.         str->string[str->len] = '\0'; /* force zero termination! */
  191.     
  192.     if(MALLOC_STATIC == type || ident->flags&FPL_EXPORT_SYMBOL) {
  193.       /*
  194.        * The previous memory was static, which probably means that
  195.        * this is an exported or global variable which required
  196.        * static existance. Swap it back to that state!
  197.        */
  198.       SwapMem(scr,
  199.                   ident->data.variable.var.str[item],
  200.                   MALLOC_STATIC);
  201.     }
  202.         else {
  203.           /*
  204.            * Make sure this allocation is dynamic!
  205.            */
  206.       SwapMem(scr,
  207.                   ident->data.variable.var.str[item],
  208.                   MALLOC_DYNAMIC);
  209.         }
  210.       }
  211.       else {
  212.     *(char **)(*tags) = NULL;
  213.       }
  214.       break;
  215.  
  216.     case FPLREF_GET_INTEGER:
  217.       if(ident->flags&FPL_INT_VARIABLE &&
  218.      item<ident->data.variable.size) {
  219.     /* we supply a pointer to it, then we can return NULL if there was
  220.        something that looked fishy! */
  221.         *(long **)(*tags) = &ident->data.variable.var.val32[item];
  222.       }
  223.       else {
  224.     *(long **)(*tags) = NULL;
  225.       }
  226.       break;
  227.  
  228.     case FPLREF_SET_INTEGER:
  229.       if(ident->flags&FPL_INT_VARIABLE &&
  230.          item<ident->data.variable.size) {
  231.     /* set integer variable item */
  232.         ident->data.variable.var.val32[item] = (*tags);
  233.       }
  234.       break;
  235.     }
  236.     tags++;
  237.   }
  238.   return ret;
  239. }
  240.  
  241.